//
//  CourseMainController.m
//  Arts1
//
//  Created by apple on 15/9/21.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "CourseMainController.h"
#import "CourseMainCollectionViewCell.h"
#import "AllHeader.h"


#define kURL @"http://arts1.dlab.com.cn/app/mycourselist"
@interface CourseMainController ()
// 数据源
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation CourseMainController

static NSString * const reuseIdentifier = @"Cell";
- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[CourseMainCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.backgroundColor = kBackgroundColor;
    
    [self requestData];
    
    // Do any additional setup after loading the view.
}

/**
 *  请求数据
 */
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    
    NSString *uid = dic[@"data"][@"uid"];
    
    NSString *key = dic[@"data"][@"key"];
    
    NSDictionary *pardic = [NSDictionary dictionary];
    
    if (uid && key)
    {
        pardic = @{@"uid":uid,
                   @"key":key};
    }
    
    [manage POST:kURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             //返回的状态
             int index = [dic[@"status"] intValue];
             if (index == 1) {
                 _dataArr = nil;
                 NSArray *arr = dic[@"list"];
                 for (NSDictionary *dict in arr) {
                     CourseModel *course = [[CourseModel alloc] init];
                     [course setValuesForKeysWithDictionary:dict];
                     [self.dataArr addObject:course];
                 }
             }
             
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self.collectionView reloadData];
             });
             // 请求成功的时候调用这个block
             NSLog(@"数据请求--%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CourseMainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    CourseModel *model = _dataArr[indexPath.row];
    cell.course = model;
    // Configure the cell
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //实现一个页面跳转，实现方法在UserViewController页面的创建课程视图方法里面
    if (_playcourseblock)
    {
        CourseModel *model = _dataArr[indexPath.row];
        _playcourseblock(model);
    }
}
@end
